
public class ReflectTest {
    //反射
    //1. 一个类只能拥有一个class对象
    public static void main(String[] args) throws ClassNotFoundException {
        //1. 使用 Class.forName("类的全路径名")
        Class<?> c1 = Class.forName("Student2");
        //2. .class方法
        Class<?> c2 = Student2.class;
        //3. 使用类对象的class
        Student2 student2 = new Student2();
        Class<?> c3 = student2.getClass();

        System.out.println(c1.equals(c2));
        System.out.println(c3.equals(c2));
        System.out.println(c1.equals(c3));

        /*
        true
        true
        true
        */

    }

}

class Student2 {
    //私有属性name
    private String name = "bit";
    //公有属性age
    public int age = 18;

    //不带参数的构造方法
    public Student2() {
        System.out.println("Student()");
    }

    private Student2(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("Student(String,name)");
    }

    private void eat() {
        System.out.println("i am eat");
    }

    public void sleep() {
        System.out.println("i am pig");
    }

    private void function(String str) {
        System.out.println(str);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}





